<template>
  <div class="preview-bill">

    <div class="preview-box1">
      <span>姓名：{{name}}</span>
      <span>身份证号码：{{certNo}}</span>
      <span>银行卡号：{{cardNo}}</span>
      <span>银行名称：{{bankName}}</span>
    </div>

    <div class="preview-box1">
      <span>企业名称：{{company}}</span>
      <span>离职日期：{{workEndTime}}</span>
      <span>离职方式：{{workEndType}}</span>
      <span>员工来源：{{userOrigin}}</span>
    </div>

    <div class="preview-box2">
      <span>本月工时1及工价1：{{currentWorkHours}}小时&nbsp;&nbsp;{{currentWorkPrice}}元/每小时</span>
      <span v-if="currentWorkHours2>0 && currentWorkPrice2>0">本月工时2及工价2：{{currentWorkHours2}}小时&nbsp;&nbsp;{{currentWorkPrice2}}元/每小时</span>
      <div class="pre-box" v-if="lastWorkHours && lastWorkPrice">
        <span>上月工时1及工价1：{{lastWorkHours}}小时&nbsp;&nbsp;{{lastWorkPrice}}元/每小时</span>
      </div>
      <div class="pre-box" v-if="lastWorkHours2>0 && lastWorkPrice2>0">
        <span>上月工时2及工价2：{{lastWorkHours2}}小时&nbsp;&nbsp;{{lastWorkPrice2}}元/每小时</span>
      </div>
      <span class="total-money">应发工资：{{totalMoney}}元</span>
      <span class="lend-money" v-if="onlineLendMoney">蓝聘线上借支：-{{onlineLendMoney}}元</span>
      <span v-if="withholdList.length" v-for="item in withholdList">{{item.withhold}}：-{{item.withholdMoney}}元</span>
      <span v-if="subsidyList.length" v-for="item in subsidyList">{{item.subsidy}}：+{{item.subsidyMoney}}元</span>
      <span class="money">实发工资：{{gainMoney}}元</span>
    </div>

    <div class="preview-box3">
      <span>人力来源：{{userSource}}</span>
      <span>发放方式：{{provideType == 1?'转员工':'转代理'}}</span>
      <div class="proxy-box" v-if="provideType == 2">
        <span>代理姓名：{{agencyName}}</span>
        <span>代理身份证号：{{agencyCardNumber}}</span>
        <span>代理银行卡号：{{agencyBankNum}}</span>
        <span>代理银行名称：{{agencyBankName}}</span>
        <span>代理银行卡归属地：{{agencyBankAddress}}</span>
      </div>
    </div>

    <div class="btn-box">
      <button type="button" class="btn cancel" @click="backPage">返回修改</button>
      <button type="button" class="btn confirm" @click="leaveBill">保存</button>
    </div>
  </div>
</template>

<script>
  import {toDecimal} from "../../../config/util"
  import {baseUrl} from "../../../config/env"

  export default {
    data() {
      return {
        certNo: "",
        cardNo: "",
        bankName: "",
        userId: "",
        mechanismId: "",
        company: "",
        workEndTime: "",//离职时间
        tempEndTime: "",
        workEndType: "",//离职类型  1合同到期  2 辞退、开除  3 辞工
        userOrigin: "",
        currentWorkHours: "",//本月工时（小时）
        currentWorkPrice: "",// 本月工价（元/小时）
        currentWorkHours2: "",//本月工时（小时）
        currentWorkPrice2: "",// 本月工价（元/小时）
        lastWorkHours: "",//上月工时（小时）
        lastWorkPrice: "",//上月工价（元/小时）
        lastWorkHours2: "",//上月工时（小时）
        lastWorkPrice2: "",//上月工价（元/小时）
        onlineLendMoney: "",//蓝聘线上借支
        deductContent: "",//扣款项目
        subsidyContent: "",//补贴项目
        userSource: "",//人力来源
        provideType: "",//工资发放方式 1转员工 2转代理
        agencyName: "",//代理姓名
        agencyCardNumber: "",//代理身份证
        agencyBankNum: "",//代理银行卡号
        agencyBankName: "",//代理银行名称
        agencyBankAddress: "",//代理银行卡归属地
        totalMoney: "",//应发工资
        withholdList: [],
        subsidyList: [],
        gainMoney: "",
        userSign: "",
        name: "",

        jobSubsidy: "",//岗位津贴
        nightSubsidy: "",//夜班津贴
        hotelSubsidy: "",//住宿补助
        monthAward: "",//月度绩效奖
        eatSubsidy: "",//伙食补助
        attendanceBonus: "",//全勤奖
        lastMonthSubsidy: "",//上月补发
        otherSubsidy: "",//其他补贴

        compensate: "",//赔偿
        eatFee: "",//伙食费
        waterPowerFee: "",//水电网络费
        premium: "",//保险
        overdueFee: "",//合同未到期扣款
        absenteeismFee: "",//旷工扣款
        clothFee: "",//工衣鞋扣款
        borrowLendMoney: "",//线下借支
        withholdLendMoney: "",//代扣借支
        lastMonthBalance: "",//上月负数
        incomeTax:"",//个人所得税
        otherFee: "",//其他扣款
      }
    },
    mounted() {
      let base64 = require('js-base64').Base64;
      this.userSign = base64.decode(sessionStorage.getItem("success"));
      this.name = this.$route.query.name;
      this.certNo = this.$route.query.certNo;
      this.cardNo = this.$route.query.cardNo;
      this.bankName = this.$route.query.bankName;
      this.userId = this.$route.query.userId;
      this.mechanismId = this.$route.query.mechanismId;
      this.company = this.$route.query.company;
      this.tempEndTime = this.$route.query.workEndTime;
      if (this.$route.query.workEndTime) {
        this.workEndTime = `${this.$route.query.workEndTime.substr(0, 4)}年${this.$route.query.workEndTime.substr(5, 2)}月${this.$route.query.workEndTime.substr(8, 2)}日`;
      }
      this.changeWorkEndType(this.$route.query.workEndType);
      this.changeEmpOrigin(this.$route.query.originType);
      this.currentWorkHours = this.$route.query.currentWorkHours;
      this.currentWorkPrice = this.$route.query.currentWorkPrice;
      this.currentWorkHours2 = this.$route.query.currentWorkHours2;
      this.currentWorkPrice2 = this.$route.query.currentWorkPrice2;
      this.lastWorkHours = this.$route.query.lastWorkHours;
      this.lastWorkPrice = this.$route.query.lastWorkPrice;
      this.lastWorkHours2 = this.$route.query.lastWorkHours2;
      this.lastWorkPrice2 = this.$route.query.lastWorkPrice2;
      if (this.$route.query.onlineLendMoney) {
        this.onlineLendMoney = this.$route.query.onlineLendMoney;
      }
      this.userSource = this.$route.query.userSource;
      this.provideType = this.$route.query.provideType;
      this.agencyName = this.$route.query.agencyName;
      this.agencyCardNumber = this.$route.query.agencyCardNumber;
      this.agencyBankNum = this.$route.query.agencyBankNum;
      this.agencyBankName = this.$route.query.agencyBankName;
      this.agencyBankAddress = this.$route.query.agencyBankAddress;
      this.totalMoney = toDecimal(parseFloat(this.currentWorkHours * this.currentWorkPrice + this.currentWorkHours2 * this.currentWorkPrice2 + this.lastWorkHours * this.lastWorkPrice + this.lastWorkHours2 * this.lastWorkPrice2));
      this.withholdList = JSON.parse(sessionStorage.getItem("withholdList"));
      this.subsidyList = JSON.parse(sessionStorage.getItem("subsidyList"));
      //处理扣款记录
      let withHoldMoney = 0;
      let temp = "";
      if (this.withholdList != null && this.withholdList.length > 0) {
        for (let i = 0; i < this.withholdList.length; i++) {
          withHoldMoney += parseFloat(this.withholdList[i].withholdMoney);
          temp += this.withholdList[i].withhold + "-" + this.withholdList[i].withholdMoney + ";";//住宿费-300;社保-50

          this.changeWithholdText(this.withholdList[i].withhold, this.withholdList[i].withholdMoney);
        }
        this.deductContent = temp;
      }

      //处理补贴
      let subsidyMoney = 0;
      let short = "";
      if (this.subsidyList != null && this.subsidyList.length > 0) {
        for (let i = 0; i < this.subsidyList.length; i++) {
          subsidyMoney += parseFloat(this.subsidyList[i].subsidyMoney);
          short += this.subsidyList[i].subsidy + "-" + this.subsidyList[i].subsidyMoney + ";";//住宿费-300;社保-50

          this.changeSubsidyText(this.subsidyList[i].subsidy, this.subsidyList[i].subsidyMoney);
        }
        this.subsidyContent = short;
      }

      this.gainMoney = this.totalMoney - this.onlineLendMoney - withHoldMoney + subsidyMoney;
    },
    methods: {
      changeWithholdText(text, money) {
        if (text == "赔偿") {//赔偿
          this.compensate = money;
        } else if (text == "伙食费") {//伙食费
          this.eatFee = money;
        } else if (text == "水电网络费") {//水电网络费
          this.waterPowerFee = money;
        } else if (text == "保险") {//保险
          this.premium = money;
        } else if (text == "合同未到期扣款") {//合同未到期扣款
          this.overdueFee = money;
        } else if (text == "旷工扣款") {//旷工扣款
          this.absenteeismFee = money;
        } else if (text == "工衣鞋扣款") {//工衣鞋扣款
          this.clothFee = money;
        } else if (text == "线下借支") {//线下借支
          this.borrowLendMoney = money;
        } else if (text == "代扣借支") {//代扣借支
          this.withholdLendMoney = money;
        } else if (text == "上月负数") {//上月负数
          this.lastMonthBalance = money;
        } else if(text == "个人所得税"){//个人所得税
          this.incomeTax = money;
        } else if (text == "其他扣款") {//其他扣款
          this.otherFee = money;
        }
      },
      changeSubsidyText(text, money) {
        if (text == "岗位津贴") {//岗位津贴
          this.jobSubsidy = money;
        } else if (text == "夜班津贴") {//夜班津贴
          this.nightSubsidy = money;
        } else if (text == "住宿补助") {//住宿补助
          this.hotelSubsidy = money;
        } else if (text == "月度绩效奖") {//月度绩效奖
          this.monthAward = money;
        } else if (text == "伙食补助") {//伙食补助
          this.eatSubsidy = money;
        } else if (text == "全勤奖") {//全勤奖
          this.attendanceBonus = money;
        } else if (text == "上月补发") {//上月补发
          this.lastMonthSubsidy = money;
        } else if (text == "其他补贴") {//其他补贴
          this.otherSubsidy = money;
        }
      },
      changeEmpOrigin(value){
        if ("DH" == value) {
          this.userOrigin = "大和";
        } else if ("LP" == value) {
          this.userOrigin = "蓝聘";
        } else if ("SS" == value) {
          this.userOrigin = "上势";
        } else if ("DYH" == value) {
          this.userOrigin = "大赢和";
        }
      },
      changeWorkEndType(value){
        if ("1" == value) {
          this.workEndType = "合同到期";
        } else if ("2" == value) {
          this.workEndType = "辞退、开除";
        } else if ("3" == value) {
          this.workEndType = "辞工";
        }
      },
      backPage() {//修改
        this.$router.push({path: "/leavebill", query: {"userId": this.userId}});
      },
      leaveBill() {//保存
        this.$vux.loading.show({//查询实名信息以及借支金额
          text: '加载中',
        });
        let data = {
          "userId": this.userId,
          "mechanismId": this.mechanismId,//企业id
          "workEndTime": this.tempEndTime,//离职时间
          "workEndType": this.$route.query.workEndType,  //离职类型  1合同到期  2 辞退、开除  3 辞工
          "userOrigin": this.$route.query.originType,//员工来源
          "currentWorkHours": this.currentWorkHours,//本月工时（小时）
          "currentWorkPrice": this.currentWorkPrice,// 本月工价（元/小时）
          "currentWorkHours2": this.currentWorkHours2,//本月工时（小时）
          "currentWorkPrice2": this.currentWorkPrice2,// 本月工价（元/小时）
          "lastWorkHours": this.lastWorkHours, //上月工时（小时）
          "lastWorkPrice": this.lastWorkPrice,//上月工价（元/小时）
          "lastWorkHours2": this.lastWorkHours2, //上月工时（小时）
          "lastWorkPrice2": this.lastWorkPrice2,//上月工价（元/小时）
          "onlineLendMoney": this.onlineLendMoney, //蓝聘线上借支
          "deductContent": this.deductContent, //扣款项目
          "subsidyContent": this.subsidyContent, //补贴项目
          "userSource": this.userSource,  //人力来源
          "provideType": this.provideType,  //工资发放方式 1转员工 2转代理
          "agencyName": this.agencyName,  //代理姓名
          "agencyCardNumber": this.agencyCardNumber,  //代理身份证
          "agencyBankNum": this.agencyBankNum, //代理银行卡号
          "agencyBankName": this.agencyBankName,  //代理银行名称
          "agencyBankAddress": this.agencyBankAddress,//代理银行卡归属地

          "jobSubsidy": this.jobSubsidy,//岗位津贴
          "nightSubsidy": this.nightSubsidy,//夜班津贴
          "hotelSubsidy": this.hotelSubsidy,//夜班津贴
          "monthAward": this.monthAward,//月度绩效奖
          "eatSubsidy": this.eatSubsidy,//伙食补助
          "attendanceBonus": this.attendanceBonus,//全勤奖
          "lastMonthSubsidy": this.lastMonthSubsidy,//上月补发
          "otherSubsidy": this.otherSubsidy,//其他补贴

          "compensate": this.compensate,//赔偿
          "eatFee": this.eatFee,//伙食费
          "waterPowerFee": this.waterPowerFee,//水电网络费
          "premium": this.premium,//保险
          "overdueFee": this.overdueFee,//合同未到期扣款
          "absenteeismFee": this.absenteeismFee,//旷工扣款
          "clothFee": this.clothFee,//工衣鞋扣款
          "borrowLendMoney": this.borrowLendMoney,//线下借支
          "withholdLendMoney": this.withholdLendMoney,//代扣借支
          "incomeTax": this.incomeTax,//个人所得税
          "lastMonthBalance": this.lastMonthBalance,//上月负数
          "otherFee": this.otherFee//其他扣款
        };

        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/insert_resign_record?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);

        this.axios.post(baseUrl + "/resident/insert_resign_record?sgin=" + this.userSign + "&auth=" + signature + "&timestamp=" + time, data).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0) {
            if (resultObj.data == 0) {
              this.$vux.toast.text("离职单保存失败，请重试", "middle");
            } else {
              sessionStorage.removeItem("withholdList");
              sessionStorage.removeItem("subsidyList");
              sessionStorage.removeItem("leaveEndTime");
              sessionStorage.removeItem("leave");
              this.$router.push({path: "/leavesearch"});
            }
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      }
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../assets/stylus/mixin.styl"
  .preview-bill
    wh(100%, 100%)
    background: rgba(242, 242, 242, 1)
    .preview-box1
      display: flex
      flex-direction: column
      justify-content: center
      margin-bottom: 0.267rem
      wh(100%, auto)
      background: rgba(255, 255, 255, 1)
      span
        margin-left: 0.32rem
        padding-top: 0.24rem
        padding-bottom: 0.24rem
        size-color(0.427rem, rgba(153, 153, 153, 1))
        &:first-child
          size-color(0.453rem, rgba(51, 51, 51, 1))
          font-weight: 500
    .preview-box2
      display: flex
      flex-direction: column
      justify-content: center
      margin-bottom: 0.267rem
      wh(100%, auto)
      background: #FFF
      span
        margin-left: 0.32rem
        padding-top: 0.2rem
        padding-bottom: 0.2rem
        size-color(0.427rem, rgba(153, 153, 153, 1))
        &:first-child
          padding-top: 0.467rem
      .pre-box
        display: flex
        flex-direction: column
        span
          margin-left: 0.32rem
          padding-top: 0.2rem
          padding-bottom: 0.2rem
          size-color(0.427rem, rgba(153, 153, 153, 1))
      .total-money
        font-weight: 500
        color: rgba(102, 102, 102, 1)
      .lend-money
        padding-top: 0.64rem
      .money
        padding-bottom: 0.48rem
        size-color(0.453rem, rgba(60, 175, 255, 1))
        font-weight: 500
    .preview-box3
      display: flex
      flex-direction: column
      justify-content: center
      wh(100%, auto)
      background: rgba(255, 255, 255, 1)
      span
        margin-left: 0.32rem
        padding-top: 0.2rem
        padding-bottom: 0.2rem
        size-color(0.427rem, rgba(153, 153, 153, 1))
        &:first-child
          padding-top: 0.48rem
        &:last-child
          padding-bottom: 0.747rem
      .proxy-box
        display: flex
        flex-direction: column
    .btn-box
      display: flex
      justify-content: center
      align-items: center
      padding-top: 0.96rem
      padding-bottom: 0.64rem
      width: 100%
      height: 1.28rem
      background: rgba(242, 242, 242, 1)
      .btn
        width: 4.267rem
        height: 1.28rem
        line-height: 1.28rem
        font-size: 0.453rem
        border-radius: 0.16rem
        color: rgba(255, 255, 255, 1)
      .cancel
        margin-right: 0.227rem
        background: rgba(255, 83, 83, 1)
      .confirm
        margin-left: 0.227rem
        background: rgba(60, 175, 255, 1)
</style>
